<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <router-link to="/login" tag="button">请登录</router-link>
        <router-view></router-view>
    </div>

    <template id="home">
        <div>
            <h3>这里是主页</h3>
        </div>
    </template>

    <template id="login">
        <div>
            用户名：<input type="text" placeholder="请输入用户名"><br>
            密码：<input type="password" placeholder="请输入密码">
            <button @click="loginS">登录</button>
        </div>
    </template>

    <script src="../lib/Vue/vue.js"></script>
    <script src="../lib/Vue/vue-router.js"></script>
    <script>

        const router = new VueRouter({
            routes: [
                {
                    path: '/',
                    redirect: '/login'
                },
                {
                    path: '/login',
                    component: {
                        template: '#login',
                        methods: {
                            loginS() {
                                sessionStorage.setItem('token', '2433asddsfsd')
                            }
                        },
                    }
                },
                {
                    path: '/home',
                    component: {
                        template: '#home'
                    }
                }
            ]
        })

        router.beforeEach((to, from, next) => {
            var token = sessionStorage.getItem('token');
            if (to.path == '/home' && !token) {
                next('/login')
            } else {
                next()
            }
        })

        var vm = new Vue({
            el: '#app',
            router
        })
    </script>
</body>
</html>